package com.itqf.service.impl;

import com.itqf.dao.AccountDao;
import com.itqf.entity.Account;
import com.itqf.service.AccountService;
import org.springframework.aop.framework.AopContext;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/9/29
 * @Time: 下午3:27
 */
public class AccountServiceImpl implements AccountService {

    AccountDao accountDao;



    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(String fromAccount, String toAccount, double money) {
        //调用业务层另一个方法

        Account accountFrom = accountDao.findById(fromAccount);
        Account accountTo = accountDao.findById(toAccount);

        accountFrom.setAccount_balance(accountFrom.getAccount_balance()-money);

        int i =  accountDao.update(accountFrom);//转出

        accountTo.setAccount_balance(accountTo.getAccount_balance()+money);
        // int  k = 10/0;  没有事务，不会回滚

        i += accountDao.update(accountTo);//转入
        System.out.println(i);

        try {//嵌套的事务  被调用的业务方法需要 try -catch

            //this.update();  调用的是被代理对象（没有事务）

            //要得到当前类的代理对象
            AccountService accountService = (AccountService)
                    AopContext.currentProxy();
            accountService.update();

        }catch (Exception e){

        }


    }

    @Override
    public   void  update(){
        Account accountFrom = accountDao.findById("4");
        Account accountTo = accountDao.findById("3");

        accountFrom.setAccount_balance(accountFrom.getAccount_balance()-100);

        int i =  accountDao.update(accountFrom);//转出

        accountTo.setAccount_balance(accountTo.getAccount_balance()+100);

        int  k = 10/0;
        System.out.println(k);

        i += accountDao.update(accountTo);//转入
        System.out.println(i);
    }




}
